package com.yl.pattern.structuralmodel.proxy.cglib_proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Author 小羊
 * @Date 30/10/2024下午 8:11
 * @ClassName ProxyFactory
 * @Description 代理对象工厂，用来获取对象
 */
public class ProxyFactory implements MethodInterceptor {
    //声明火车站对象
    private TrainStation station = new TrainStation();
    public TrainStation getProxyObject(){
        //创建Enhancer对象，类似JDK代理中的Proxy类
        Enhancer enhancer = new Enhancer();
        //设置父类的字节码对象
        enhancer.setSuperclass(TrainStation.class);
        //设置回调函数
        enhancer.setCallback(this);
        //创建代理对象
        TrainStation station = (TrainStation) enhancer.create();
        return station;
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("代售点收取服务费用（CGLib代理）");
        Object obj = method.invoke(station, objects);
        return obj;
    }
}
